//
//  SEGPropertyAppraisalCollectionViewCell.m
//  SEGUhomePropertyBaseService
//
//  Created by Clavis on 2018/12/10.
//  Copyright © 2018 刘山. All rights reserved.
//

#import "SEGPropertyAppraisalCollectionViewCell.h"

@interface SEGPropertyAppraisalCollectionViewCell ()

@property (nonatomic, strong) UIImageView *headImageView;
@property (nonatomic, strong) UILabel *nameLabel;
@property (nonatomic, strong) UILabel *positionLabel;
@property (nonatomic, strong) UILabel *totalStarLabel;

@end

@implementation SEGPropertyAppraisalCollectionViewCell

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        [self setupUI];
    }
    return self;
}

- (void)setupUI {
    self.headImageView = ({
        UIImageView *imageView = [[UIImageView alloc] init];
        [self addSubview:imageView];
        imageView.image = [UIImage imageWithContentsOfFile:SEGPicName(@"headimg160")];
        imageView.contentMode = UIViewContentModeScaleAspectFill;
        imageView.layer.cornerRadius = 5;
        imageView.layer.masksToBounds = YES;
        imageView;
    });
    [self.headImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(10);
        make.centerX.mas_equalTo(0);
        make.width.height.mas_equalTo(64);
    }];
    
    self.nameLabel = ({
        UILabel *lable = [[UILabel alloc] init];
        [self addSubview:lable];
        lable.textColor = kColor_1;
        lable.font = kFont_13;
        lable.textAlignment = NSTextAlignmentCenter;
        lable;
    });
    [self.nameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.headImageView.mas_bottom).offset(10);
        make.left.mas_offset(12);
        make.right.mas_offset(-12);
    }];
    
    self.positionLabel = ({
        UILabel *lable = [[UILabel alloc] init];
        [self addSubview:lable];
        lable.textColor = kColor_3;
        lable.font = kFont_11;
        lable.textAlignment = NSTextAlignmentCenter;
        lable;
    });
    [self.positionLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.nameLabel.mas_bottom).offset(5);
        make.left.equalTo(self.nameLabel.mas_left);
        make.right.equalTo(self.nameLabel.mas_right);
    }];
    
    self.totalStarLabel = ({
        UILabel *lable = [[UILabel alloc] init];
        [self addSubview:lable];
        lable.textColor = kColor_3;
        lable.font = kFont_11;
        lable;
    });
    [self.totalStarLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.positionLabel.mas_bottom).offset(10);
        make.centerX.equalTo(self.headImageView);
    }];
    
    self.backgroundColor = kColor_W2;
    
    // 添加阴影
    self.layer.cornerRadius = 4;
    self.layer.shadowColor = [UIColor.blackColor colorWithAlphaComponent:0.4].CGColor;
    self.layer.shadowOffset = CGSizeMake(0, 0);
    self.layer.shadowOpacity = 0.2;
    self.layer.shadowRadius = 4;
    UIBezierPath *shadowPath = [UIBezierPath bezierPathWithRect:self.bounds];
    //设置阴影路径
    self.layer.shadowPath = shadowPath.CGPath;
}

- (void)updateCellWithModel:(SEGExcellentStaffModel *)model {
    [self.headImageView setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@%@",kClientImageRootURL,model.empeePhoto ?: @""]] placeholderImage:[UIImage imageWithContentsOfFile:SEGPicName(@"headimg160")]];
    self.nameLabel.text = model.empeeName;
    self.positionLabel.text = model.empeeJob;
    self.totalStarLabel.text = [NSString stringWithFormat:@"获得%@个赞", [self formatLikeNumber:model.likeNum]];
}

- (NSString *)formatLikeNumber:(NSInteger)likeNumber {
    if (likeNumber >= 1000) {
        return [NSString stringWithFormat:@"%.1lfk", likeNumber/1000.0];
    }
    return [NSString stringWithFormat:@"%ld", (long)likeNumber];
}

@end
